//
//  ConfNoticeViewController.m
//  Conference
//
//  Created by 陈鑫 on 12-11-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//大会通知

#import "ConfNoticeViewController.h"
#import "AlarmData.h"
#import "NetContant.h"
#import "Globals.h"
#import "Constant.h"
#import "MainDataBase.h"
#import "WebData.h"
#import "DateTool.h"
#import "AlarmCellController.h"
#import "AlarmTimeCellController.h"
@interface ConfNoticeViewController ()

@end

@implementation ConfNoticeViewController
@synthesize table;
@synthesize arrayOfData;
//@synthesize tableHight;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadingData];

    
    
}


-(void)loadingData{
    //self.arrayOfData=[NSMutableArray arrayWithCapacity:0];
    [self getDataFromDB];
    
}


-(void)getDataFromDB{
    
    
    self.arrayOfData=[[MainDataBase getInstance ] getAlarms];

}

//-(void)getDataFromDB{
//
//    [self.arrayOfData removeAllObjects];
//    
//    NSMutableArray *alarmGroups=[[MainDataBase getInstance]getAlarmsGroup];
//
//    NSDate *latsDate=nil;
//    for (NSString *groupDate in alarmGroups) {
//        NSDate *date=[[DateTool getInstance]getDateForString:groupDate];
//        
//        if(nil==latsDate){
//            AlarmData *alarmData=[[AlarmData alloc]init];
//            alarmData.isDate=YES;
//            alarmData.alarmDate=[[DateTool getInstance]getTimeByDuration:groupDate];
//            latsDate=date;
//            [self.arrayOfData addObject:alarmData];
//            [alarmData release];
//        }else{
//            
//            NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//            unsigned int unitFlags = NSDayCalendarUnit;
//            NSDateComponents *comps = [gregorian components:unitFlags fromDate:latsDate  toDate:date  options:0];
//            int days = [comps day];
//            [gregorian release];
//            if (days!=0) {
//                
//                AlarmData *alarmData=[[AlarmData alloc]init];
//                alarmData.isDate=YES;
//                alarmData.alarmDate=[[DateTool getInstance]getTimeByDuration:groupDate];
//                latsDate=date;
//                [self.arrayOfData addObject:alarmData];
//                [alarmData release];
//            }
//            
//        }
//        
//        
//     NSMutableArray *dateAlarmList=[[MainDataBase getInstance ] getAlarms];
//
//    [self.arrayOfData addObjectsFromArray:dateAlarmList];
//    }
//}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AlarmData *data = [arrayOfData objectAtIndex:indexPath.row];
//    if(data.isDate){
//          UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
//           return cell.frame.size.height;
//    }else{
    
        CGSize size=[[Globals getInstance]CalculationCellSize:280 fontSize:16 content:data.message];
        return 30+size.height+8;
    
    
//    }
    
   
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arrayOfData count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
            static NSString *norCell=@"alarmCell";
            
            AlarmCellController *cell=(AlarmCellController *)[tableView dequeueReusableCellWithIdentifier:norCell];
          if(cell ==nil){
               NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"AlarmCellController" owner:self options:nil];   
               for (id oneObject in array) {
                    if ([oneObject isKindOfClass:[AlarmCellController class]]) {
                        cell=oneObject;
                        break;
                    }
                }
              
              cell.backgroundView=[[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"msg_bg_normal.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20]] autorelease];
              
              cell.selectedBackgroundView=[[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"msg_bg_pressed.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20]] autorelease];
    
            }
            
            AlarmData *data= [self.arrayOfData objectAtIndex:indexPath.row];
    
           cell.lab_time.text=[NSString stringWithFormat:@"时间：%@",data.dateTime];
         
            
           CGSize size=[[Globals getInstance]CalculationCellSize:280 fontSize:16 content:data.message];
            
            CGRect rect = [cell.lab_content textRectForBounds:cell.lab_content.frame limitedToNumberOfLines:0];
            rect.size=size;
            cell.lab_content.frame = rect;
            cell.lab_content.text=data.message;
            cell.lab_content.font = [UIFont systemFontOfSize:16];
            cell.lab_content.numberOfLines=0;
            return cell;
        }


//-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    AlarmData *data=[self.arrayOfData objectAtIndex:[indexPath row]];
//    if(data.isDate){
//        static NSString *alarmTimeCell=@"alarmTimeCell";
//        AlarmTimeCellController *timeCell=(AlarmTimeCellController *)[tableView dequeueReusableCellWithIdentifier:alarmTimeCell];
//        
//        if(timeCell ==nil){
//            NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"AlarmTimeCellController" owner:self options:nil];   
//            for (id oneObject in array) {
//                if ([oneObject isKindOfClass:[AlarmTimeCellController class]]) {
//                    timeCell=oneObject;
//                    break;
//                }
//            }
//            timeCell.selectionStyle=UITableViewCellSelectionStyleNone;
//        }
//        
//        timeCell.lab_time.text=data.alarmDate;
//  //      self.tableHight=self.tableHight+20;
//        return timeCell;
//    }else {
//        static NSString *norCell=@"norCell";
//        
//        AlarmCellController *cell=(AlarmCellController *)[tableView dequeueReusableCellWithIdentifier:norCell];
//        if(cell ==nil){
//            NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"AlarmCellController" owner:self options:nil];   
//            for (id oneObject in array) {
//                if ([oneObject isKindOfClass:[AlarmCellController class]]) {
//                    cell=oneObject;
//                    break;
//                }
//            }
//            cell.selectionStyle=UITableViewCellSelectionStyleNone;
//
//        }
//        
//        cell.lab_time.text=data.dateTime;
//      
//        cell.lab_title.text=data.title;
//        
//        UIFont *font = [UIFont systemFontOfSize:16];
//        
//       CGSize size=[[Globals getInstance]CalculationCellSize:self.table.frame.size.width fontSize:16 content:data.message];
//        
//        CGRect rect = [cell.lab_content textRectForBounds:cell.lab_content.frame limitedToNumberOfLines:0];
//        rect.size=size;
//        cell.lab_content.frame = rect;
//        cell.lab_content.text=data.message;
//        cell.lab_content.numberOfLines = 0;
//        cell.lab_content.font = font;
//        
//        //设置时间在底部
//        cell.lab_time.frame=CGRectMake(185, rect.size.height+40, 130, 20);
//        
////        self.tableHight=self.tableHight+size.height+20;
//        
//        
////        UIImage *img=[UIImage imageNamed:@".png"];
////        [img stretchableImageWithLeftCapWidth:1 topCapHeight:1];
////        [cell.img_bg setFrame:CGRectMake(0, 0, 320, size.height+20)];
////        
////        [cell.img_bg setImage:img];
//        
//           return cell;
//    }
//    
//}

-(void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView1 deselectRowAtIndexPath:indexPath animated:NO];
}


- (void)viewDidUnload
{
    [self setTable:nil];
    [self setArrayOfData:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
    [arrayOfData release];
    [table release];
    [super dealloc];
}
@end
